package mr.ali.videoapp.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import mr.ali.videoapp.portal.Exception.ServiceException;
import mr.ali.videoapp.portal.mapper.CollectionPlayUrlMapper;
import mr.ali.videoapp.portal.model.CollectionPlay;
import mr.ali.videoapp.portal.model.CollectionPlayUrl;
import mr.ali.videoapp.portal.service.ICollectionPlayCllectService;
import mr.ali.videoapp.portal.service.ICollectionPlayService;
import mr.ali.videoapp.portal.service.ICollectionPlayUrlService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Mr-ali
 * @since 2025-07-18
 */
@Service
public class CollectionPlayUrlServiceImpl extends ServiceImpl<CollectionPlayUrlMapper, CollectionPlayUrl> implements ICollectionPlayUrlService {
    @Resource
    private CollectionPlayUrlMapper collectionPlayUrlMapper;

    @Resource
    private ICollectionPlayService collectionPlayService;

    @Resource
    private ICollectionPlayCllectService collectionPlayCllectService;

    /**
     * 随机获取
     */
    @Override
    public PageInfo<CollectionPlayUrl> findAllByOrderLastUrl(Integer pageNum, Integer pageSize , Integer userId) {

        // 判断翻页参数是否为空
        if (pageNum == null || pageSize == null) {
            // 抛出翻页参数错误异常
            throw ServiceException.GONE("翻页参数错误");
        }
        // 创建查询条件包装器
        LambdaQueryWrapper<CollectionPlayUrl> wrapper = new LambdaQueryWrapper<>();
        wrapper.last("ORDER BY RAND()");
        // 使用PageHelper进行分页
        PageHelper.startPage(pageNum, pageSize);
        // 根据查询条件获取视频列表
        List<CollectionPlayUrl> videoList = collectionPlayUrlMapper.selectList(wrapper);

        // Java 层去重
        List<CollectionPlayUrl> uniqueTags = videoList.stream()
                .collect(Collectors.collectingAndThen(
                        Collectors.toMap(
                                CollectionPlayUrl::getId,
                                item -> item,
                                (existing, replacement) -> existing
                        ),
                        map -> new ArrayList<>(map.values())
                ));

        // ✅ 使用 PageHelper 自动封装的 PageInfo（不是你自己 new 的！）

        for (CollectionPlayUrl video : uniqueTags) {
            List<CollectionPlay> collectionPlay = collectionPlayService.videoTagsById(video.getVideoId());
            Long row = collectionPlayCllectService.CountPlayCommentIndex(video.getId());
            Boolean now = collectionPlayCllectService.SelectNewCollect(userId, video.getId());
            video.setCollectionPlays(collectionPlay);
            video.setCollects(row);
            video.setIfCollect(now);
        }
        PageInfo<CollectionPlayUrl> pageInfo = new PageInfo<>(videoList);
        pageInfo.setList(uniqueTags);
        return pageInfo;
    }

    /**
     * 计算集数
     */
    @Override
    public Long CountPlayUrlIndex(Integer videoId) {
        QueryWrapper<CollectionPlayUrl> wrapper = new QueryWrapper<>();
        wrapper.eq("video_id", videoId);
        Long row = collectionPlayUrlMapper.selectCount(wrapper);
        return row;
    }


    @Override
    public PageInfo<CollectionPlayUrl> collectionPlayList(Integer pageNum , Integer pageSize , Integer userId , Integer videoId) {
        QueryWrapper<CollectionPlayUrl> wrapper = new QueryWrapper<>();
        wrapper.eq("video_id", videoId);
        wrapper.orderByAsc("video_ji");
        // 使用PageHelper进行分页
        PageHelper.startPage(pageNum, pageSize);
       List<CollectionPlayUrl> uniqueTags = collectionPlayUrlMapper.selectList(wrapper);
        for (CollectionPlayUrl video : uniqueTags) {
            List<CollectionPlay> collectionPlay = collectionPlayService.videoTagsById(video.getVideoId());
            Long row = collectionPlayCllectService.CountPlayCommentIndex(video.getId());
            Boolean now = collectionPlayCllectService.SelectNewCollect(userId, video.getId());
            video.setCollectionPlays(collectionPlay);
            video.setCollects(row);
            video.setIfCollect(now);
        }
        return  new PageInfo<>(uniqueTags);
    }
}
